using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for PictureSizes.
	/// </summary>
	public class PictureSizes : GalleryThemedControl
	{
		private const string resFormat = "{0} x {1}";
	
		#region Child Controls
		
		private HyperLink ThumbnailLink;
		private IText ThumbnailRes;

		private HyperLink SmallLink;
		private IText SmallRes;
		
		private HyperLink MediumLink;
		private IText MediumRes;
		
		private HyperLink LargeLink;
		private IText LargeRes;
		
		private HyperLink OriginalLink;
		private IText OriginalRes;
		
		#endregion

		#region Public Properties

		private GalleryPost dataSource;
		public GalleryPost DataSource
		{
			get { return dataSource; }
			set { dataSource = value; }
		}

		#endregion

		#region Skin


		protected override void AttachChildControls()
		{
			ThumbnailLink = (HyperLink)FindControl( "ThumbnailLink" );
			ThumbnailRes = FindText( "ThumbnailRes" );

			SmallLink = (HyperLink)FindControl( "SmallLink" );
			SmallRes = FindText( "SmallRes" );

			MediumLink = (HyperLink)FindControl( "MediumLink" );
			MediumRes = FindText( "MediumRes" );

			LargeLink = (HyperLink)FindControl( "LargeLink" );
			LargeRes = FindText( "LargeRes" );

			OriginalLink = (HyperLink)FindControl( "OriginalLink" );
			OriginalRes = FindText( "OriginalRes" );

		}

		#endregion

		#region Event Overrides

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad (e);
            this.DataBind();
        }


		public override void DataBind()
		{
			
			base.DataBind();
			
			BindData();

		}

		#endregion

		private void BindData()
		{
			if(DataSource == null)
				DataSource = GalleryPosts.GetPicture(CSContext.Current.PostID); //return;

			//Make sure all these images exist in the cache, if not create them

			if(this.ThumbnailLink != null)
			{
				GalleryImage image = new GalleryImage(GalleryImageType.Thumbnail, DataSource);
				image.ProcessPicture() ;

				ThumbnailLink.NavigateUrl = image.ImageUrl;
				ThumbnailLink.Text = ResourceManager.GetString( "Gallery_PictureSizes_Thumbnail" );

				if(this.ThumbnailRes != null)
					ThumbnailRes.Text = string.Format(resFormat, image.Width , image.Height);
			}

			if(this.SmallLink != null)
			{
				GalleryImage image = new GalleryImage(GalleryImageType.SecondaryThumbnail, DataSource);
				image.ProcessPicture() ;
				
				SmallLink.NavigateUrl = image.ImageUrl;
				SmallLink.Text = ResourceManager.GetString( "Gallery_PictureSizes_Small" );

				if(this.SmallRes != null)
					SmallRes.Text = string.Format(resFormat, image.Width , image.Height);
			}
		
			if(this.MediumLink != null)
			{
				GalleryImage image = new GalleryImage(GalleryImageType.Details, DataSource);
				image.ProcessPicture() ;
				
				MediumLink.NavigateUrl = image.ImageUrl;
				MediumLink.Text = ResourceManager.GetString( "Gallery_PictureSizes_Medium" );

				if(this.MediumRes != null)
					MediumRes.Text = string.Format(resFormat, image.Width , image.Height);
			}
		
			if(this.LargeLink != null)
			{
				GalleryImage image = new GalleryImage(GalleryImageType.Slideshow, DataSource);
				image.ProcessPicture() ;

				LargeLink.NavigateUrl = image.ImageUrl;
				LargeLink.Text = ResourceManager.GetString( "Gallery_PictureSizes_Large" );

				if(this.LargeRes != null)
					LargeRes.Text = string.Format(resFormat, image.Width , image.Height);
			}
		
			if(this.OriginalLink != null)
			{
				OriginalLink.NavigateUrl = GalleryUrls.Instance().PictureUrl(DataSource);
				OriginalLink.Text = ResourceManager.GetString( "Gallery_PictureSizes_Original" );

				if(this.OriginalRes != null && DataSource.Attachment != null)
					OriginalRes.Text = string.Format(resFormat, DataSource.Attachment.Width, DataSource.Attachment.Height);
			}
		}

	}

}
